package imitate.mybatis.code.generation.creating.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import imitate.mybatis.code.generation.creating.BaseCreatingFile;
import imitate.mybatis.code.generation.data.base.po.DataBaseTable;
import imitate.mybatis.code.generation.field.CreateEntityFileConfig;
import imitate.mybatis.code.generation.parsing.ApiXmlFileParser;
import imitate.mybatis.code.generation.parsing.XNode;
import imitate.mybatis.code.generation.session.Configuration;

/**
 * impl 类文件生成类
 * 
 * @author Administrator
 *
 */
public abstract class ServletCreatingFile extends BaseCreatingFile {
	/**
	 * 日志
	 */
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	protected CreateEntityFileConfig fileConfig;

	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	public CreateEntityFileConfig getFileConfig() {
		return fileConfig;
	}

	public void setFileConfig(CreateEntityFileConfig fileConfig) {
		this.fileConfig = fileConfig;
	}

	public ServletCreatingFile(Configuration configuration) {
		super(configuration);
	}

	public void createFileProcess() {
		// 第一步解析。要生成的文件的xml的配置
		analysisFile();
		// 第二步 循环生成内容、文件
		List<DataBaseTable> fileList = this.configuration.getNeedCreateFileTableList();
		fileList.stream().forEach(this::createContent);
		//

		//
	}

	// 文件生成
	public void createContent(DataBaseTable table) {
		this.fileConfig.setTable(table);

		// 解析 properties 文件，IDao,IService,方法名、报名等都需要自己解析才能使用
		analysisProperties();
		// 删除上一个文件的内容
		content.delete(0, content.length());
		// 生成文件主题
		// StringBuilder content = new StringBuilder();
		String bodyCodeContent = createContentBodyCode();
		content.append(bodyCodeContent);
		// 生成文件
		createFile();
	}

	public String createContentBodyCode() {
		XNode fileBody = this.fileConfig.getFileBody();
		return ApiXmlFileParser.parse(fileBody.getStringBody(), pro);
	}

	// 解析 properties 参数
	public void analysisProperties() {
		createProperties(this.fileConfig);
	}

	@Override
	public void createProperties(Object paramObj) {
		super.createProperties(paramObj);
	}

	// 解析xml配置
	public abstract void analysisFile();

	// 解析 生成文件
	public abstract void createFile();
}
